#!/usr/bin/perl

use strict;

open(my $fh, '<', "HillstromData.csv") or die $!;
open(my $output, '>', "ParsedData.csv") or die $!;

my $line = <$fh>;

while(!eof($fh))
{
    $line = <$fh>;
    $line =~ /(\d+),"*(\d).*?,(\d+\.*\d*),(\d),(\d),(\w+),(\d),(\w+),([A-Za-z \-]+),(\d),(\d),(\d+\.*\d*)/;  
    
    my $rec = $1;
    my $hist_seg = $2;
    my $hist = $3;
    my $mens = $4;
    my $womens = $5;
    my $zip = $6;
    my $newb = $7;
    my $chan = $8;
    my $seg = $9;
    my $visit = $10;
    my $conv = $11;
    my $spend = $12;

    if($zip =~ /Rural/)
    {
        $zip = 0;
    }
    elsif($zip =~ /Surburban/)
    {
        $zip = 1;
    }
    elsif($zip =~ /Urban/)
    {
        $zip = 2;
    }

    if($chan =~ /Phone/)
    {
        $chan = 0;
    }
    elsif($chan =~ /Web/)
    {
        $chan = 1;
    }
    elsif($chan =~ /Multichannel/)
    {
        $chan = 2;
    }

    if($seg =~ /No E-Mail/)
    {
        $seg = 0;
    }
    elsif($seg =~ /Mens E-Mail/)
    {
        $seg = 1;
    }
    elsif($seg =~ /Womens E-Mail/)
    {
        $seg = 2;
    }

    print $output "$rec,$hist_seg,$hist,$mens,$womens,$zip,$newb,$chan,$seg,$visit,$conv,$spend\n";
}

close($fh);
